package com.neteast.androidclient.contact;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import org.json.JSONObject;
import com.neteast.androidclient.contact.activity.MainActivity;
import com.neteast.androidclient.contact.domain.Contact;
import com.neteast.androidclient.contact.domain.ContactData;
import com.neteast.androidclient.contact.util.ContactsUtils;
import com.neteast.androidclient.contact.util.DownloadTask;
import com.neteast.androidclient.contact.util.JsonUtil;
import com.neteast.androidclient.contact.util.LoginUtil;
import com.neteast.androidclient.contact.util.NetUtil;
import com.neteast.androidclient.contact.util.PinYinUtil;

import android.os.Environment;
import android.test.AndroidTestCase;
import android.util.Log;

public class Test extends AndroidTestCase {
	
	public void testContact() throws Throwable {
		Contact contact=getContact("张三");
		ContactData data=new ContactData();
		data.id=2;
		data.label="公司";
		data.content="01098978876";
		data.modified=Constant.CONTACT_NEW;
		contact.phones.add(data);
		
		contact.groups="2,3";
		contact.phones.get(0).id=4;
		
	}
	public static Contact getContact(String name) {
		Contact contact=new Contact();
		contact.ownId=1;
		contact.contactId=2;
		contact.displayName=name;
		contact.organization="东方网信";
		contact.job="首席大门执行官";
		contact.photo="/mnt/sdcard/photo/1.png";
		contact.hasPhoto=true;
		contact.remark="看门的";
		contact.state="在东方网信看大门中";
		contact.groups="家人,朋友";
		
		ArrayList<ContactData> phones=new ArrayList<ContactData>();
		
		ContactData data=new ContactData();
		data.id=0;
		data.label="手机";
		data.content="12345677543";
		data.modified=Constant.CONTACT_NEW;
		phones.add(data);
		
		data=new ContactData();
		data.id=1;
		data.label="住宅";
		data.content="010-87686321";
		data.modified=Constant.CONTACT_NEW;
		phones.add(data);
		
		data=new ContactData();
		data.id=2;
		data.label="公司";
		data.content="01098978876";
		data.modified=Constant.CONTACT_NEW;
		phones.add(data);
		
		
		data=new ContactData();
		data.id=3;
		data.label="其他";
		data.content="918380689@163.com";
		data.modified=Constant.CONTACT_NEW;
		data.mimeTypeId=Constant.MIME_EMAIL;
		contact.emails.add(data);
		
		Collections.sort(phones);
		contact.phones.addAll(phones);
		
		return contact;
	}
	
	public void testSysContacts() throws Throwable {
		NetUtil.importSystemContacts(getContext());
	}
	public void testTranslate() throws Throwable {
		Contact contact=new Contact();
		JSONObject json = JsonUtil.translateContactToJson(contact);
		Log.i("test", json.toString());
	}
	
	public void testDao() throws Throwable{
		String displayName = ContactsUtils.getDisplayName(getContext(), "64");
		Log.i("test", "displayName="+displayName);
	}
	
	public void testUpdate() throws Throwable {
		new DownloadTask(getContext()).execute(null);
	}
	public void testCloudFriends() throws Throwable{
		ArrayList<Contact> cloudFriends = NetUtil.getCloudFriends(getContext(), LoginUtil.getLastLoginUserId(getContext()));
		if (cloudFriends!=null) {
			Log.i("test", MainActivity.cloudFriends.toString());
		}
	}
	public void testContactFriends() throws Throwable{
		ArrayList<Contact> contactFriends = NetUtil.getContactFriends(getContext(), LoginUtil.getLastLoginUserId(getContext()));
		if (contactFriends!=null) {
			Log.i("test", MainActivity.contactFriends.toString());
		}
	}
	public void testPinYin() throws Throwable{
		Log.i("test",PinYinUtil.hanzi2PinYin("张三08"));
		Log.i("test",PinYinUtil.hanzi2PinYinOnlyHead("张三08"));
	}
	public void testSearch() throws Throwable{
		for(int i=0;i<10;i++){
			MainActivity.contactFriends.add(getContact("张三"+i));
		}
		ArrayList<Contact> list = NetUtil.searchContacts(0, "918380689");
		int size = list.size();
		for(int i=0;i<size;i++){
			Log.i("test", list.get(i).toString());
		}
	}
	
	public void testDelContact() throws Throwable{
		LoginUtil.setLastLoginUserId(getContext(), 51);
		LoginUtil.setLastLoginToken(getContext(), "A1B2C3D4E5F6");
		Contact contact=new Contact();
		contact.contactId=3879;
		NetUtil.deleteContact(getContext(),contact);
	}
	public void testEditContact() throws Throwable{
		JSONObject obj=new JSONObject();
		obj.put("contactId", "3870");
		obj.put("femail", "tianya@163.com");
		obj.put("sortKey", "X");
		Contact contact = NetUtil.editContact(getContext(), obj);
		Log.i("test", contact.toString());
	}
	public void testgetContactDetail() throws Throwable{
		Contact contact = NetUtil.getContactFromServer(getContext(), 3878);
		Log.i("test", contact.toString());
	}
	public void testAddContact() throws Throwable{
		Contact contact=new Contact();
		contact.fphone="13468795414";
		contact.displayName="墨玉";
		contact.ownId=51;
		contact.sortKey=PinYinUtil.getSortKey(contact.displayName);
		JSONObject obj = JsonUtil.translateContactToJson(contact);
		contact = NetUtil.addContact(getContext(), obj);
		Log.i("test", contact.toString());
	}
	
	public void testUploadPhoto() throws Throwable {
		File file=new File(Environment.getExternalStorageDirectory(), "DCIM/Camera/C360_2012-03-01-09-26-04.jpg");
		String uploadPhoto = NetUtil.uploadPhoto(getContext(),file);
		Log.i("test", "uploadPhoto="+uploadPhoto);
	}
	public void testgetContactFriends() throws Throwable {
		ArrayList<Contact> list = NetUtil.getContactFriends(getContext(), 51);
		Log.i("test", list.toString());
	}
	public void testgetCloudFriends() throws Throwable {
		ArrayList<Contact> list = NetUtil.getCloudFriends(getContext(), 51);
		Log.i("test", list.toString());
	}
	public void testAddContactGroup() throws Throwable {
		NetUtil.addContactGroup(getContext(), "部落");
	}
	public void testAddCloudGroup() throws Throwable {
		NetUtil.addCloudGroup(getContext(), "末日战狼");
	}
	public void testDeleteGroup() throws Throwable {
		NetUtil.getContactGroups(getContext(), 51);
		NetUtil.deleteGroup(getContext(), "部落", NetUtil.CONTACTS);
	}
	public void testgetContactGroup() throws Throwable {
		ArrayList<String> contactGroups = NetUtil.getContactGroups(getContext(), 51);
		Log.i("test", "contactGroups="+contactGroups.toString());
	}
	public void testgetCloudGroup() throws Throwable {
		ArrayList<String> cloudGroups = NetUtil.getCloudGroups(getContext(), 51);
		Log.i("test", "cloudGroups="+cloudGroups.toString());
	}
	public void testEditCloudGroup() throws Throwable {
		ArrayList<String> cloudGroups = NetUtil.getCloudGroups(getContext(), 51);
		NetUtil.editGroup(getContext(), "云好友新", "圣杯骑士团", NetUtil.CLOUD);
	}
	public void testEditContactGroup() throws Throwable {
		ArrayList<String> contactGroups = NetUtil.getContactGroups(getContext(), 51);
		NetUtil.editGroup(getContext(), "英雄联盟", "部落", NetUtil.CONTACTS);
	}
	public void	testDownApk() throws Throwable{
		NetUtil.downloadNewSoftware(getContext(), 1.1f);
	}
}
